<?php

/*
 * +----------------------------------------------------------------------
 * 作 者: xinglou <xinglou1@foxmail.com>
 * +----------------------------------------------------------------------
 * 创建日期：2015-06-24
 * +----------------------------------------------------------------------
 * 文件描述：系统页面管理
 * +----------------------------------------------------------------------
 * 升级记录：
 * +----------------------------------------------------------------------
 */
class PageAction extends CommonAction
{

    /**
     * 页面数据显示
     */
    public function index()
    {
        $model = new PageModel();
        // 查询系统所有页面
        $data = $model->field("id,
								parentid,
								name,
								url,
								mark,
								get_field_dict_name('page', 'target', target) target,
								ico,
								seq,
								status")
            ->order('seq')
            ->select();
        if (! empty($data)) {
            foreach ($data as $value) {
                $value['str_manage'] .= '<a href="' . U('Admin/Page/add', array(
                    'parentid' => $value['id']
                )) . '" class="btn btn-success btn-xs mbs"><i class="fa fa-plus"></i> 添加子栏目</a>&nbsp;<a href="' . U('Admin/Page/edit', array(
                    'parentid' => $value['parentid'],
                    'id' => $value['id']
                )) . '" class="btn btn-blue btn-xs mbs"> <i class="fa fa-edit"></i> 修改</a>&nbsp;<a href="javascript:confirm_link_del(' . "'" . U('Admin/Page/delete', array(
                    'id' => $value['id']
                )) . "'" . ')" class="btn btn-danger btn-xs mbs"><i class="fa fa-trash-o"></i> 删除</a>';
                $value['modulename'] = $value['parentid'] == '0' ? '<font color="green">顶级页面</font>' : '<font color="red">子页面</font>';
                $value['status_name'] = $value['status'] == a ? '<font color="green">显示</font>' : '<font color="red">隐藏</font>';
                $array[] = $value;
            }
            $str = "<tr>
						<td align='center'><input name='seq' value='\$seq' type='text' class='form-control' style='text-align:center; width:46px; padding:2px 3px;' /></td>
						<td><i class='fa \$ico'></i></td>
						<td>&nbsp;&nbsp;\$spacer&nbsp;\$name &nbsp;</td>
						<td>\$url</td>
						<td>\$mark</td>
						<td>\$modulename</td>
						<td>\$status_name</td>
						<td>\$target</td>
						<td>\$str_manage</td>
					</tr>";
            import('@.ORG.Tree');
            $tree = new Tree($array);
            unset($array);
            $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
            $categorys = $tree->get_tree('0', $str);
            $this->assign('categorys', $categorys);
        }
        $this->display();
    }

    /**
     * 添加页面显示
     */
    public function add()
    {
        $parentid = $this->_request('parentid');
        if ($parentid == NULL) {
            $parentid = '0';
        }
        $selected = $parentid; // 当前应选中的页面
        $model = new PageModel();
        $data = $model->field('id,
								parentid,
								name,
								seq')
            ->order('seq')
            ->select();
        import('@.ORG.Tree');
        $str = "<option value='\$id' \$selected>\$spacer [\$seq] \$name</option>";
        $tree = new Tree($data);
        $categorys = $tree->get_tree('0', $str, $selected);
        $this->assign('categorys', $categorys);
        // 数据字典列表解析
        $this->assign('target_list', set_field_dict_list('page', 'target'));
        $this->assign('status_list', set_field_dict_list('page', 'status'));
        $this->display();
    }

    /**
     * 将页面数据添加至数据库
     */
    public function insert()
    {
        $model = new PageModel();
        if (false !== $model->create()) {
            if (false !== $model->add()) {
                redirect(__APP__ . '?g=Admin&m=Page&a=index');
            } else {
                $this->error('页面添加失败！');
            }
        } else {
            $this->error($model->getError());
        }
    }

    /**
     * 修改页面显示
     */
    public function edit()
    {
        $id = $this->_request('id'); // 要修改的页面的id
        $selected = $this->_request('parentid');
        $model = new PageModel();
        $data = $model->where("id = '" . $id . "'")->find();
        $this->assign('data', $data);
        $page = $model->field('id,
								parentid,
								name,
								intro,
								seq')
            ->order('seq')
            ->select();
        import('@.ORG.Tree');
        $str = "<option value='\$id' \$selected>\$spacer [\$seq] \$name</option>";
        $tree = new Tree($page);
        $categorys = $tree->get_tree('0', $str, $selected);
        $this->assign("categorys", $categorys);
        
        // 数据字典列表解析
        $this->assign('target_list', set_field_dict_list('page', 'target'));
        $this->assign('status_list', set_field_dict_list('page', 'status'));
        $this->display();
    }

    /**
     * 将页面数据更新至数据库
     */
    public function update()
    {
        $model = new PageModel();
        if (false !== $model->create()) {
            if (false !== $model->save()) {
                redirect(__APP__ . '?g=Admin&m=Page&a=index');
            } else {
                $this->error('页面修改失败！');
            }
        } else {
            $this->error($model->getError());
        }
    }

    /**
     * 删除页面数据
     */
    public function delete()
    {
        $model = new PageModel();
        if (false !== $model->where("id = '" . $this->_request('id') . "'")
            ->delete()) {
            redirect($_SERVER['HTTP_REFERER']);
        } else {
            $this->error('页面删除失败！');
        }
    }
}
?>